package com.tronsis.distribution.activity.me;

import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.tronsis.distribution.R;
import com.tronsis.distribution.bean.UserBean;
import com.tronsis.distribution.biz.factory.UserBizFactory;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.db.DBUtil;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.util.StringUtil;
import com.tronsis.library.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NickNameActivity extends ToolBarBaseActivity {


    @BindView(R.id.et_nick_name)
    EditText mEtNickName;
    @BindView(R.id.btn_submit)
    Button mBtnSubmit;

    final UserBean mLoginUser = UserBizFactory.createBiz().getLoginUser(mContext);

    private String type;
    private String hint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nick_name);
        ButterKnife.bind(this);
        type = getIntent().getType();
        if (type == null) {
            return;
        }

        int maxLength = 16;

        String title = "";
        String text = "";
        switch (type) {
            case "nickname":
                title = getString(R.string.update_nick_name);
                text = mLoginUser.getName();
                maxLength = 16;
                hint = "请输入昵称";
                break;
            case "id":
                title = getString(R.string.update_id_num);
                text = mLoginUser.getIdCardNo();
                maxLength = 18;
                hint = "请输入身份证号";
                break;
            case "wechat":
                title = getString(R.string.update_wechat);
                text = mLoginUser.getWechatId();
                maxLength = 32;
                hint = "请输入微信号";
                break;
            default:
                break;
        }
        setTitle(true, title);
        InputFilter[] fArray = new InputFilter[1];
        fArray[0] = new InputFilter.LengthFilter(maxLength);
        mEtNickName.setFilters(fArray);
        mEtNickName.setText(text);
        mEtNickName.setHint(hint);
        mEtNickName.setSelection(mEtNickName.getText().length());

    }

    @OnClick(R.id.btn_submit)
    void submitNickName() {
        String nickName = mEtNickName.getText().toString();
        if (TextUtils.isEmpty(nickName)) {
            ToastUtil.showMessage(mContext, hint);
            return;
        }
        updateNickName(nickName);
    }


    private void updateNickName(final String text) {
        String nickName = null;
        String idNum = null;
        String wechat = null;
        switch (type) {
            case "nickname":
                nickName = text;
                break;
            case "id":
                idNum = text;
                if (!StringUtil.isIDNumber(idNum)) {
                    ToastUtil.showMessage(mContext, "请输入正确的身份证号码");
                    return;
                }
                break;
            case "wechat":
                wechat = text;
                break;
            default:
                break;
        }



        try {
            UserBizFactory.createBiz().updateInfo(this, mLoginUser.isAppNotice(), null, idNum, null, nickName, null, null, null, wechat, null, new UICallBack() {

                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    switch (type) {
                        case "nickname":
                            mLoginUser.setName(text);
                            break;
                        case "id":
                            mLoginUser.setIdCardNo(text);
                            break;
                        case "wechat":
                            mLoginUser.setWechatId(text);
                            break;
                        default:
                            break;
                    }

                    DBUtil.getInstance(mContext).update(mLoginUser);
                    setResult(RESULT_OK);
                    finish();
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
